package com.fan.teamer.model.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Team;
import com.fan.teamer.model.domain.TeamMember;
import com.fan.teamer.model.domain.TeamMemberId;

public class TeamMemberDaoImpl implements TeamMemberDao {

	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 * 
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 * 
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public TeamMember getTeamMember(TeamMemberId id) {
		TeamMember teamMember = (TeamMember) sessionFactory.getCurrentSession()
				.get(TeamMember.class, id);
		return teamMember;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TeamMember> getAllTeamMembers() {
		return sessionFactory.getCurrentSession()
				.createCriteria(TeamMember.class).list();
	}

	@Override
	public int getSize() {
		return getAllTeamMembers().size();
	}

	@Override
	public void addTeamMember(TeamMember teamMember) {
		sessionFactory.getCurrentSession().save(teamMember);
	}

	@Override
	public void updateTeamMember(TeamMember teamMember) {
		sessionFactory.getCurrentSession().update(teamMember);
	}

	@Override
	public void deleteTeamMember(TeamMember teamMember) {
		sessionFactory.getCurrentSession().delete(teamMember);
	}

	@Override
	public boolean hasTeamMember(TeamMemberId id) {
		if (getTeamMember(id) == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public List<String> getColumnNames() {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> getMembersEmailListByTeam(Team team) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				TeamMember.class);

		criteria.add(Restrictions.eq("id.teamId", team.getId()));

		List<String> teamMembers = new ArrayList<String>();
		for (TeamMember teamMember : (List<TeamMember>) (criteria.list())) {
			teamMembers.add(teamMember.getId().getMemberEmail());
		}
		return teamMembers;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> getTeamsIdListByMember(Member member) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				TeamMember.class);
		criteria.add(Restrictions.eq("id.memberEmail", member.getEmail()));

		List<Integer> memberTeams = new ArrayList<Integer>();
		for (TeamMember teamMember : (List<TeamMember>) (criteria.list())) {
			
			memberTeams.add(teamMember.getId().getTeamId());
		}

		return memberTeams;
	}

	@Override
	public String getMemberRoleInTeam(Member member, Team team) {
		TeamMemberId teamMemberId = new TeamMemberId();
		teamMemberId.setMemberEmail(member.getEmail());
		teamMemberId.setTeamId(team.getId());

		return getTeamMember(teamMemberId).getMemberRole();
	}

	@SuppressWarnings("unchecked")
	@Override
	public void deleteByMember(Member member) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				TeamMember.class);
		criteria.add(Restrictions.eq("id.memberEmail", member.getEmail()));
		List<TeamMember> teamMembers = (List<TeamMember>) criteria.list();
		for (TeamMember teamMember : teamMembers) {
			deleteTeamMember(teamMember);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void deleteByTeam(Team team) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				TeamMember.class);
		criteria.add(Restrictions.eq("id.teamId", team.getId()));

		List<TeamMember> teamMembers = (List<TeamMember>) criteria.list();
		for (TeamMember teamMember : teamMembers) {
			deleteTeamMember(teamMember);
		}
	}

	@Override
	public TeamMember getTeamMember(Team team, Member member) {
		TeamMemberId teamMemberId = new TeamMemberId();
		teamMemberId.setMemberEmail(member.getEmail());
		teamMemberId.setTeamId(team.getId());
		return getTeamMember(teamMemberId);
	}

}
